package com.zqweb.gulimall.order.web;

import com.alipay.api.AlipayApiException;
import com.zqweb.common.exception.SkuWareException;
import com.zqweb.gulimall.order.config.AlipayTemplate;
import com.zqweb.gulimall.order.service.OrderService;
import com.zqweb.gulimall.order.vo.OrderConfirmVo;
import com.zqweb.gulimall.order.vo.OrderSubmitRespVo;
import com.zqweb.gulimall.order.vo.OrderSubmitVo;
import com.zqweb.gulimall.order.vo.PayVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.concurrent.ExecutionException;

@Controller
public class OrderWebController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private AlipayTemplate alipayTemplate;

    @GetMapping(value = "/orderPay",produces = "text/html")
    @ResponseBody
    /*
     用户点击支付后的映射接口（用于构建提交支付表单的PayVo，并生成相应的表单html代码，
     以text/html的格式响应回浏览器，且实现该表单的自动提交）完成的功能：
     1、将支付页让浏览器展示
     2、支付成功以后，再跳到用户的订单列表页（return_url）
      */
    public String orderPay(@RequestParam("orderSn") String orderSn) throws AlipayApiException {
        PayVo payVo = orderService.orderPay(orderSn);
        String res = alipayTemplate.pay(payVo);
        return res;
    }

    @GetMapping("/toTrade")
    // 去结算页面
    public String toTrade(Model model) throws ExecutionException, InterruptedException {
        OrderConfirmVo orderConfirmVo = orderService.getOrderConfirmVo();
        model.addAttribute("orderConfirmData", orderConfirmVo);
        return "confirm";
    }

    @PostMapping("/submitOrder")
    // 订单提交接口
    public String submitOrder(OrderSubmitVo orderSubmitVo,Model model,RedirectAttributes redirectAttributes) {
        System.out.println("提交的订单信息为：" + orderSubmitVo);
        /**
         * 下单任务：创建订单、验证令牌（防重）、验证价格、锁定库存等
         * 下单结果：下单成功，则跳转到支付页面   下单失败，则回到订单确认页重新确认订单信息
         */
        OrderSubmitRespVo orderSubmitRespVo = new OrderSubmitRespVo();
        try {
            orderSubmitRespVo = orderService.orderSubmitVo(orderSubmitVo);
        } catch (SkuWareException e) {
            orderSubmitRespVo.setHandleStatus(3);// 库存容量不足
        }
        System.out.println("提交订单得到的OrderSubmitRespVo为：\n" + orderSubmitRespVo);
        Integer status = orderSubmitRespVo.getHandleStatus();
        if(status == 1){
            model.addAttribute("orderInfo",orderSubmitRespVo);
            // 带上orderSubmitRespVo
            return "pay";
        }else{
            String msg = "提交订单失败：";
            switch (status){
                // RedirectAttributes调用addFlashAttribute是往session里头存放信息，同样也会在Request域中也存放一份
                case 0:
                    redirectAttributes.addFlashAttribute("orderErrMsg",msg + "订单信息过期，请刷新后再次提交");break;
                case 2:
                    redirectAttributes.addFlashAttribute("orderErrMsg",msg + "订单商品价格发生变化，请确认后再次提交");break;
                case 3:
                    redirectAttributes.addFlashAttribute("orderErrMsg",msg + "库存锁定失败，库存不足");
            }
            return "redirect:http://order.gulimall.com/toTrade";
        }

    }


}
